import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDletStateChangeException;

public class Menu implements CommandListener {

	private static Display display;
    private static Form form;
    static Menu midlet;
    private TextBox text;
    private String comment;
    
    // Formularios
	private static final String BACK = "Voltar";
	private static final String VISIT_BLOGS = "Visitar Blogs";
    private static final String COMMENT = "Comentar";
        
    // Comandos
    Command enter;
    Command exit;
    Command save;
    Command back;
        
    String username;
    private boolean logged;
        
    List listBlogao;
    private static final int LIST_BLOGS = 0;
    private static final int LIST_COMMENT = 1;
    private static final int LIST_BACK = 2;
        
    public Menu() {
    	comment = "";
    	display = Main.getDisplay();
    	display.setCurrent(form);
    	midlet = this;
         
    	listBlogao = new List("Blogao", List.IMPLICIT);
    	listBlogao.append(BACK, null);
    	listBlogao.append(VISIT_BLOGS, null);
    	listBlogao.append(COMMENT, null);
                
    	enter = new Command("Selecionar", Command.ITEM, 0);
    	save = new Command("Salvar", Command.ITEM, 0);
    	exit = new Command("Voltar", Command.BACK, 0);
    	back = new Command("Voltar", Command.BACK, 0);

    	listBlogao.addCommand(enter);
    	listBlogao.addCommand(exit);
    	listBlogao.setCommandListener(this);
    }

    public void commandAction(Command command, Displayable d) {
    	if (command == enter) {
    		if (logged) {
    			switch (listBlogao.getSelectedIndex()) {
    			case 1:
    				comment();
    				break;
    			case LIST_BACK:
    				backToMain();
    				break;
    			}
    		}
        } else {
        	if (Login.validateLogin()) {
        		getDisplay().setCurrent(listBlogao);
        		logged = true;
        	}
        }
        if (command == save) {
        	if (listBlogao.getSelectedIndex() == 1) {
        		viewText(comment);
            }
        }
        if (command == exit) {
        	backToMain();
        }
        if (command == back) {
        	switch (listBlogao.getSelectedIndex()) {
            case LIST_BLOGS:
            	getDisplay().setCurrent(listBlogao);
            	break;
            case LIST_COMMENT:
            	getDisplay().setCurrent(listBlogao);
            	break;
            case LIST_BACK:
            	getDisplay().setCurrent(listBlogao);
                break;
            }
        }
    }

    private void comment() {
    	text = new TextBox("Seu comentario:", "", 100, TextField.ANY);
    	text.addCommand(save);
    	text.addCommand(back);
    	text.setCommandListener(this);
    	getDisplay().setCurrent(text);
    }

    private void viewText(String texto) {
    	texto = text.getString();
        Alert alert = new Alert("Texto salvo", texto, null, AlertType.INFO);
        alert.addCommand(back);
        alert.setCommandListener(this);
        getDisplay().setCurrent(alert);
    }

    public void backToMain() {
    	Menu.getDisplay().setCurrent(Main.getListBlogao());
    }

    public void view() {
    	getDisplay().setCurrent(listBlogao);
    }

    public static Display getDisplay() {
        return display;
    }

    public Displayable getOptions() {
        return listBlogao;
    }
}